package com.raymond.entity.commons;

import lombok.Data;

import java.io.Serializable;

/**
 * desc-通用返回对象
 *
 * @author houmenghe
 * @date 2025/04/20 22:44
 **/
@Data
public class DtoResult<T> implements Serializable {

    private static final long serialVersionUID = -3086157556681921256L;

    public static final int STATUS_CODE_SUCCESS = 200;

    public static final int STATUS_CODE_FAILED = 500;

    private T data;

    private int code;

    private String message;

    private boolean success;

    public DtoResult() {
        this.setCode(STATUS_CODE_SUCCESS);
    }

    public DtoResult(T data) {
        this();
        this.data = data;
    }

    public DtoResult(Integer code, String message, T data) {
        this.setCode(code);
        this.message = message;
        this.data = data;
    }

    public DtoResult(Integer code, String message) {
        this.setCode(code);
        this.message = message;
    }

    public boolean getSuccess() {
        return this.success;
    }

    public void setCode(int code) {
        this.code = code;
        this.success = this.code == STATUS_CODE_SUCCESS;
    }

    public static <T> DtoResult<T> ok(T data) {
        return new DtoResult<>(STATUS_CODE_SUCCESS, null, data);
    }

    public static DtoResult ok(String message, Object data) {
        return new DtoResult(STATUS_CODE_SUCCESS, message, data);
    }
}
